﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PMS.BLL;
using PMS.Entity;
using PMS;
using PMS.Resources;
using PMS.Win;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Base;
using PMS.Utils;

namespace PMS.ChucNang
{
    public partial class DongMoKyLookup : FormPopupBase
    {
        private readonly DongMoKyBO service = new DongMoKyBO();
        public DongMoKy DongMoKySelected;
        public DongMoKyLookup()
        {
            InitializeComponent();
            InitStyle();
            InitData();
        }

      
        void InitStyle()
        {
            grvDongMoKyList.OptionsBehavior.Editable = false;
            grvDongMoKyList.OptionsView.ShowGroupPanel = false;
            grvDongMoKyList.OptionsView.ShowIndicator = true;

            grvDongMoKyList.OptionsView.ShowAutoFilterRow = true;
            grvDongMoKyList.OptionsView.ShowFilterPanelMode = ShowFilterPanelMode.Never;
            grvDongMoKyList.OptionsView.ShowGroupedColumns = false;
            grvDongMoKyList.OptionsMenu.EnableColumnMenu = false;
            grvDongMoKyList.OptionsCustomization.AllowColumnMoving = false;
            grvDongMoKyList.OptionsCustomization.AllowGroup = false;
            grvDongMoKyList.OptionsFilter.AllowFilterEditor = false;

            this.grvDongMoKyList.OptionsSelection.MultiSelect = false;
            this.grvDongMoKyList.IndicatorWidth = Constants.INDICATOR_WIDTH;
            //gridbinding
            this.colMaKy.FieldName = DongMoKyColumns.MaKy.ToString();
            this.colNgayMo.FieldName = DongMoKyColumns.NgayMoKy.ToString();
            this.colNgayDong.FieldName = DongMoKyColumns.NgayDongKy.ToString();
            this.colGhiChu.FieldName = DongMoKyColumns.GhiChu.ToString();
            this.colTinhTrang.UnboundType = DevExpress.Data.UnboundColumnType.String;
            this.colNguoiDong.FieldName = DongMoKyColumns.NguoiCapNhap.ToString();

            //txtColTongNhap.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
            //txtColTongNhap.Mask.UseMaskAsDisplayFormat = true;
            //txtColTongNhap.Mask.EditMask = ConfigurationConstant.NumericFormatString;

            //txtColTonKho.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
            //txtColTonKho.Mask.UseMaskAsDisplayFormat = true;
            //txtColTonKho.Mask.EditMask = ConfigurationConstant.NumericFormatString;
            

        }

        void InitData()
        {
            try
            {

                this.grdDongMoKyList.DataSource = service.GetAllDongMoKy();

            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }

        }

        private void CloseDialog(DialogResult result)
        {
            try
            {
                if (result == DialogResult.OK)
                {
                    this.DongMoKySelected = (DongMoKy)grvDongMoKyList.GetFocusedRow();
                }
                else
                {
                    this.DongMoKySelected = null;
                }

                this.DialogResult = result;
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.OK);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.Cancel);
        }

        private void grvDongMoKyList_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if ((e.Info.IsRowIndicator) && (e.RowHandle >= 0))
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void grvDongMoKyList_DoubleClick(object sender, EventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo hi = view.CalcHitInfo(grdDongMoKyList.PointToClient(MousePosition));
            if (hi.InRow || hi.InRowCell)
            {
                if (DongMoKySelected != null)
                {
                    CloseDialog(DialogResult.OK);
                }
            }
        }

        private void grvDongMoKyList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                btnOk_Click(sender, null);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                btnCancel_Click(sender, null);
            }
        }

        private void grvDongMoKyList_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DongMoKySelected = this.grvDongMoKyList.GetFocusedRow() as DongMoKy;
        }

        private void grvDongMoKyList_ShowingEditor(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }

        private void grvDongMoKyList_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
        {
            DongMoKy tempRow = (DongMoKy)this.grvDongMoKyList.GetRow(e.RowHandle);
            if ((e.Column == colTinhTrang) && e.IsGetData && e.RowHandle >= 0)
            {
                if (tempRow.DongKy)
                {
                    e.Value = "Đóng";

                }
                else
                {
                    e.Value = "Mở";
                }

            }
        }
    }
}
